<template>
    <!-- 注册 -->
    <div id="in">
        <Nav></Nav>
        <div id="register">
            <h2>注册</h2>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                class="demo-ruleForm">
                <el-form-item label="账号" prop="account">
                    <el-input v-model.number="ruleForm.account"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <Footer></Footer>
    </div>
</template>

<script>
import Nav from "../components/nav.vue";
import Footer from "../components/footer.vue";
export default {
    data() {
        var checkAge = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('账号不能为空'));
            } else {
                callback()
            }
        };
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                pass: '',
                checkPass: '',
                account: ''
            },
            rules: {
                pass: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                account: [
                    { validator: checkAge, trigger: 'blur' }
                ]
            }
        }
    },
    components: {
        Nav,
        Footer
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.axios({
                        method: "post",
                        url: "/api/users/regist",
                        data: {
                            userName: this.ruleForm.account,
                            account: this.ruleForm.account,
                            password: this.ruleForm.pass
                        }
                    }).then(res => {
                        window.location.href = "/login"
                    }).catch(err => {
                        console.log(err.massage);
                        alert("失败")
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
};
</script>

<style>
#in {
    text-align: center;
    margin: 0px;
    padding: 0px;
}

#register {
    width: 80%;
    margin: 70px auto;
    padding-bottom: 50px;
    padding-top: 10px;
    background-image: url("../../public/背景.jpg");
    border-radius: 10px;
    background-size: cover;
}

.demo-ruleForm {
    width: 300px;
    margin: 20px auto;
    padding: 50px;
    border-radius: 30px;
    background-image: url("../../public/粉紫背景图.jpg");
}

h2 {
    color: palevioletred;
}
</style>